/*
 * $Id: AlphabetBar.java 536 2008-08-19 08:11:57Z jasta00 $
 *
 * Copyright (C) 2008 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.devtcg.five.music.widget;

import java.util.Map;

import org.devtcg.five.music.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Layout.Alignment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AlphabetBar extends LinearLayout
{
	private static final String TAG = "AlphabetBar";
	
	public AlphabetBar(Context context)
	{
		super(context);
		init();
	}

	public AlphabetBar(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		init();
	}

	public void init()
	{
		setLayoutParams(new LinearLayout.LayoutParams(
		  LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
		
		/* Not strictly necessary since we override onLayout and onMeasure with
		 * our custom logic, but it seems like good form to do this just to
		 * show how we're arranging the children. */
		setOrientation(VERTICAL);

		char[] labels =
		{ 
			'#',
			'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
			'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
		};

		for (int i = 0; i < labels.length; i++)
		{
			TextView label = new TextView(mContext);
			label.setText(String.valueOf(labels[i]));
			label.setGravity(Gravity.CENTER);

			label.setClickable(true);
			label.setFocusable(true);
			label.setOnClickListener(mLabelClicked);

			addView(label, new LinearLayout.LayoutParams(
			  LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		}
	}

	private OnClickListener mLabelClicked = new OnClickListener()
	{
		public void onClick(View v)
		{
			if (mClickCallback != null)
				mClickCallback.onClick(v);
		}
	};
	
	private OnClickListener mClickCallback = null;

	/**
	 * Set the click listener for alphabet labels.
	 *  
	 * @param listener
	 *   Click listener, or null to unset.
	 */
	public void setLabelClickListener(OnClickListener listener)
	{
		mClickCallback = listener;
	}
	
	private void useDefaultBackground()
	{
		setBackgroundResource(R.drawable.alphabet_bar_bg);
	}

	@Override
	protected void onMeasure(int wSpec, int hSpec)
	{
		Log.d(TAG, "onMeasure(" + wSpec + ", " + hSpec + ")");

		if (getBackground() == null)
			useDefaultBackground();

		int count = getChildCount();

		int hMode = MeasureSpec.getMode(hSpec);
		int hSize = MeasureSpec.getSize(hSpec);

		assert hMode == MeasureSpec.EXACTLY;

		int maxWidth = 0;

		int hSizeAdj = hSize - mPaddingTop - mPaddingBottom; 
		float childHeight = hSizeAdj / count;

		/* Calculate how many extra 1-pixel spaces we'll need in order to make
		 * childHeight align to integer heights. */
		int variance = hSizeAdj - ((int)childHeight * count);
		
		int paddingWidth = mPaddingLeft + mPaddingRight;

		for (int i = 0; i < count; i++)
		{
			TextView label = (TextView)getChildAt(i);

			label.setTextSize(childHeight * 0.83F);

			int thisHeight = (int)childHeight;
			
			if (variance > 0)
			{
				thisHeight++;
				variance--;
			}

			label.measure
			  (MeasureSpec.makeMeasureSpec(13, MeasureSpec.EXACTLY),
			   MeasureSpec.makeMeasureSpec(thisHeight, MeasureSpec.EXACTLY));

			maxWidth = Math.max(maxWidth, label.getMeasuredWidth());
		}

		maxWidth += paddingWidth;

		setMeasuredDimension(resolveSize(maxWidth, wSpec), hSize); 
	}	
}
